#include <bits/stdc++.h>

using namespace std;

char c[11][11];
int ax[11],ay[11],n,m,d,val[1<<11],f[11][11][1<<11],point[11],ful,ans = 0;
int dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};
bool vis[11][11][1<<11];

bool can(int x,int y){
	if(x > 0 && y > 0 && x <= n && y <= m && c[x][y] == '0') return 1;
	return 0;
}

typedef struct {int x,y,s;} node;

int newstu(int x,int y,int nx,int ny,int s){
	int ns = s;
	for(int i = 1;i <= d;i++){
		if(((ax[i] == x && ax[i] > nx) || (ax[i] == nx && ax[i] > x)) && ay[i] < ny) ns ^= 1 << (i-1);
	}
	return ns;
}

void spfa(int x,int y){
	memset(f,0x3f,sizeof(f));
	memset(vis,0,sizeof(vis));
	f[x][y][0] = 0; vis[x][y][0] = 1;
	int lx = x,ly = y;
	queue<node> q;
	q.push(node{x,y,0});
	while(q.size()){
		node st = q.front();q.pop();
		x = st.x,y = st.y;int s = st.s;
		vis[x][y][s] = 0;
		for(int i = 0;i < 4;i++){
			int nx = x + dx[i],ny = y + dy[i];
			if(!can(nx,ny)) continue;
			int ns = s;
			if(!(i & 1)) ns = newstu(x,y,nx,ny,s);
			if(f[x][y][s] + 1 < f[nx][ny][ns]){
				f[nx][ny][ns] = f[x][y][s] + 1;
				if(!vis[nx][ny][ns]) {
					q.push((node){nx,ny,ns});
					vis[nx][ny][ns] = 1;
				}
			}
		}
	}
	for(int i = 0;i <= ful;i++) ans = max(ans,val[i] - f[lx][ly][i]);
}

int main (){
	scanf("%d %d",&n,&m);
	scanf("%d",&d);
	for(int i = 1;i <= d;i++) scanf("%d",&point[i]);
	for(int i = 1;i <= n;i++){
		scanf("%s",c[i]+1);
	}
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= m;j++){
			if(isdigit(c[i][j]) && c[i][j] != '0'){
				int num = c[i][j] - 48;
				ax[num] = i;
				ay[num] = j;
			}
		}
	ful = (1<<d)-1;
	for(int i = 0;i <= ful;i++){
		for(int j = 1;j <= d;j++){
			if((i>>(j-1)&1)) {
				val[i] += point[j];
			}
		}
	}
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= m;j++)
			if(can(i,j)){
				spfa(i,j);
			}
	printf("%d\n",ans);
	return 0;
}
